package com.yunsc.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunsc.common.utils.PageResult;
import com.yunsc.common.utils.Query;
import com.yunsc.product.dao.YunscGoodsServiceDao;
import com.yunsc.product.dao.YunscGoodsServiceRelDao;
import com.yunsc.product.dto.GoodsServiceDTO;
import com.yunsc.product.entity.YunscGoodsServiceEntity;
import com.yunsc.product.service.YunscGoodsServiceService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;


@Service("yunscGoodsServiceService")
public class YunscGoodsServiceServiceImpl extends ServiceImpl<YunscGoodsServiceDao, YunscGoodsServiceEntity> implements YunscGoodsServiceService {

    @Resource
    private YunscGoodsServiceRelDao goodsServiceRelDao;

    // 查询商品服务列表
    @Override
    public PageResult<YunscGoodsServiceEntity> queryGoodsServiceList(Map<String, Object> params, Integer currentStoreId) {
        QueryWrapper<YunscGoodsServiceEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank((String) params.get("search"))) {
            queryWrapper.like("name", params.get("search"));
        }
        queryWrapper.eq("store_id", currentStoreId);
        queryWrapper.eq("is_delete", 0);
        IPage<YunscGoodsServiceEntity> page = this.page(
                new Query<YunscGoodsServiceEntity>().getPage(params), queryWrapper);
        List<YunscGoodsServiceEntity> records = page.getRecords();
        return PageResult.formatPageResult(records, (int) page.getCurrent(), (int) page.getSize(), page.getTotal());
    }

    // 查询商品服务列表
    @Override
    public List<YunscGoodsServiceEntity> queryGoodsServiceByGoodsId(Integer goodsId, Integer storeId) {
        List<Integer> serviceIds = goodsServiceRelDao.getServiceIdsByGoodsId(goodsId, storeId);
        if (serviceIds.isEmpty()) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<YunscGoodsServiceEntity> ser_lqw = new LambdaQueryWrapper<>();
        ser_lqw.eq(YunscGoodsServiceEntity::getIsDelete, 0);
        ser_lqw.in(YunscGoodsServiceEntity::getServiceId, serviceIds);
        return this.list(ser_lqw);
    }

    // 查询商品服务
    @Override
    public List<YunscGoodsServiceEntity> queryGoodsServiceAll(Integer storeId) {
        LambdaQueryWrapper<YunscGoodsServiceEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YunscGoodsServiceEntity::getStoreId, storeId);
        lqw.eq(YunscGoodsServiceEntity::getStatus, 1);
        lqw.eq(YunscGoodsServiceEntity::getIsDelete, 0);
        return this.list(lqw);
    }

    @Override
    @Transactional
    public void addGoodsService(GoodsServiceDTO goodsService, Integer storeId) {
        YunscGoodsServiceEntity goodsServiceEntity = new YunscGoodsServiceEntity();
        BeanUtils.copyProperties(goodsService.getForm(), goodsServiceEntity);
        goodsServiceEntity.setStoreId(storeId);
        this.save(goodsServiceEntity);
    }

    @Override
    @Transactional
    public void editGoodsServiceById(GoodsServiceDTO goodsService, Integer storeId) {
        YunscGoodsServiceEntity goodsServiceEntity = new YunscGoodsServiceEntity();
        BeanUtils.copyProperties(goodsService.getForm(), goodsServiceEntity);
        goodsServiceEntity.setStoreId(storeId);
        goodsServiceEntity.setServiceId(goodsService.getServiceId());
        this.updateById(goodsServiceEntity);
    }

    @Override
    @Transactional
    public void removeGoodsServiceById(Integer serviceId, Integer storeId) {
        this.update(new UpdateWrapper<YunscGoodsServiceEntity>()
                .eq("service_id", serviceId).eq("store_id", storeId)
                .set("is_delete", 1)
        );
    }

    @Override
    @Transactional
    public boolean updateGoodsServiceState(GoodsServiceDTO goodsService, Integer storeId) {
        GoodsServiceDTO.GoodsServiceForm goodsServiceForm = goodsService.getForm();
        if (goodsServiceForm == null){
            return false;
        }
        Integer isDefault = goodsServiceForm.getIsDefault();
        if (isDefault != null){
            if (isDefault == 1){
                // 修改字段为0
                return this.update(new UpdateWrapper<YunscGoodsServiceEntity>()
                        .eq("service_id", goodsService.getServiceId())
                                .eq("store_id", storeId)
                        .set("is_default", 0)
                );
            } else if (isDefault == 0){
                // 修改字段为1
                return this.update(new UpdateWrapper<YunscGoodsServiceEntity>()
                        .eq("service_id", goodsService.getServiceId())
                        .eq("store_id", storeId)
                        .set("is_default", 1)
                );
            }
        }
        Integer status = goodsServiceForm.getStatus();
        if (status != null){
            if (status == 1){
                // 修改字段为0
                return this.update(new UpdateWrapper<YunscGoodsServiceEntity>()
                        .eq("service_id", goodsService.getServiceId())
                        .eq("store_id", storeId)
                        .set("status", 0)
                );
            } else if (status == 0){
                // 修改字段为1
                return this.update(new UpdateWrapper<YunscGoodsServiceEntity>()
                        .eq("service_id", goodsService.getServiceId())
                        .eq("store_id", storeId)
                        .set("status", 1)
                );
            }
        }
        return false;
    }
}